package com.tangr.adapterapplication;

import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.tangr.normaladapter.ViewHolder;
import com.tangr.normaladapter.recyclerview.MultiItemAdapter;
import com.tangr.normaladapter.recyclerview.MultiItemTypeSupport;
import com.tangr.normaladapter.recyclerview.OnItemClickListener;
import com.tangr.normaladapter.recyclerview.PullRecyclerView;

import java.util.ArrayList;
import java.util.List;

public class PullRecyclerViewActivity extends AppCompatActivity {
    private List<String> mDatas = new ArrayList<>();
    private Handler mhandler = new Handler();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main3);

        initDatas();

        final PullRecyclerView recyclerView = (PullRecyclerView) findViewById(R.id.recyclerview);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
//        final StaggerGridAdapter<String> adapter = new StaggerGridAdapter<String>(this, mDatas, R.layout.list_item) {
//            @Override
//            public void setView(ViewHolder holder, String item) {
//                holder.setText(R.id.item_tv, item);
//                holder.setImage(R.id.item_iv, R.mipmap.ic_launcher);
//            }
//        };
        final MultiItemAdapter<String> adapter = new MultiItemAdapter<String>(this, mDatas, new MultiItemTypeSupport<String>() {
            @Override
            public int getLayoutId(int type) {
                if (type==0)
                    return R.layout.list_item;
                return R.layout.list_item1;
            }

            @Override
            public int getItemViewType(int position, String s) {
                if (position%2==0)
                    return 0;
                return 1;
            }

            @Override
            public boolean isEnabled(int viewType) {
                return true;
            }
        }) {
            @Override
            public void setView(ViewHolder holder, String item) {
                if(holder.getLayoutId()==R.layout.list_item){
                    holder.setText(R.id.item_tv,item);
                    holder.setImage(R.id.item_iv, R.mipmap.ic_launcher);
                }else {
                    holder.setText(R.id.item_tv,item);
                    holder.setImage(R.id.item_iv, R.mipmap.ic_launcher);
                }
            }
        };
        adapter.setOnItemClickListener(new OnItemClickListener<String>() {
            @Override
            public void onItemClick(ViewGroup parent, View view, String s, int position) {
                Toast.makeText(PullRecyclerViewActivity.this, "click" + position + ":" + s, Toast.LENGTH_SHORT).show();
            }

            @Override
            public boolean onItemLongClick(ViewGroup parent, View view, String s, int position) {
                Toast.makeText(PullRecyclerViewActivity.this, "longclick" + position + ":" + s, Toast.LENGTH_SHORT).show();
                return true;
            }
        });
        adapter.setHeader(R.layout.sample_header);
        adapter.setFooter(R.layout.sample_header);
        recyclerView.setAdapter(adapter);
        recyclerView.setOnRefreshListener(new PullRecyclerView.OnRecyclerRefreshListener() {
            @Override
            public void onRefresh() {
                mhandler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        mDatas.clear();
                        refreshDatas();
                        //adapter.notifyChanged();   //when StaggerGridAdapter
                        adapter.notifyDataSetChanged();
                        recyclerView.refreshFinished();
                    }
                },2000);
            }
            @Override
            public void onLoadMore() {
                mhandler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        mDatas.add("a");
                        mDatas.add("b");
                        mDatas.add("c");
//                        adapter.notifyChanged();    //when StaggerGridAdapter
                        adapter.notifyDataSetChanged();
                        recyclerView.loadFinshed();
                    }
                },2000);
            }
        });
    }

    private void initDatas()
    {
        for (int i = 'a'; i < 'z'; i++)
        {
            mDatas.add((char) i + "");
        }
    }

    private void refreshDatas()
    {
        for (int i = 'D'; i < 'N'; i++)
        {
            mDatas.add((char) i + "");
        }
    }
}
